// 文件名意思是加密、解密、哈希、随机值

import bcrypt from 'bcrypt'
import {v4 as uuidv4} from 'uuid';

export const uuid = () => {
    const strUUID = uuidv4();
    return strUUID.replace(/-/g, '');
}
const saltPrefix = '$2b$10$'
export const passwordEncrypt = (password: string) => {
    const salt = uuid()
    const hash = bcrypt.hashSync(password, saltPrefix + salt)
    return salt + hash.substring(saltPrefix.length) // 32位盐，拼上剩下的哈希
}

export const passwordCompare = (password: string, hash: string) => {
    const newHash = bcrypt.hashSync(password,saltPrefix + hash.substring(0, 32))
    return newHash.substring(saltPrefix.length) === hash.substring(32)
}
